package com.kaisa.activity.ui;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.kaisa.activity.BaseApp;
import com.kaisa.activity.R;
import com.kaisa.activity.adapter.WelcomeAdapter;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends AppCompatActivity {
    private LinearLayout linearLayout;
    private ViewPager viewPager;
    private WelcomeAdapter adapter;
    private Button btnStart;
    private List<ImageView> list = new ArrayList<ImageView>();
    private int[] imgs = {R.mipmap.welcome_1, R.mipmap.welcome_2, R.mipmap.welcome_3, R.mipmap.welcome_4};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        x.view().inject(this);
        initView();
        initData();
    }

    private void initData() {
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < imgs.length; i++) {
                    linearLayout.getChildAt(i).setEnabled(true);
                }
                linearLayout.getChildAt(position).setEnabled(false);
                if (position == 3) {
                    btnStart.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
    @Event(R.id.welcome_start)
    private void buttonEvent(View v){
        Intent intent = new Intent(WelcomeActivity.this,MainActivity.class);
        startActivity(intent);
    }
    private void initView() {
        btnStart = (Button) this.findViewById(R.id.welcome_start);
        linearLayout = (LinearLayout) this.findViewById(R.id.ll_dot);
        viewPager = (ViewPager) this.findViewById(R.id.welcome_vp);
        for (int i = 0; i < imgs.length; i++) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(params);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setImageResource(imgs[i]);
            list.add(iv);
        }
        for (int i = 0; i < imgs.length; i++) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            ImageView ivDot = new ImageView(this);
            ivDot.setLayoutParams(params);
            ivDot.setScaleType(ImageView.ScaleType.FIT_XY);
            ivDot.setImageResource(R.drawable.bg_dots);
            linearLayout.addView(ivDot);
        }
        linearLayout.getChildAt(0).setEnabled(false);
        adapter = new WelcomeAdapter(list);
        viewPager.setAdapter(adapter);
    }

}
